package com.loyal.loyalerp.basedata.action.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.action.model.GyAction;
import com.loyal.loyalerp.basedata.action.service.ActionService;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.model.vo.GridVO;


@Controller
@RequestMapping("/action")
public class ActionController{
	@Autowired
	ActionService actionService;
	
	final static String tableName = "gy_action";

	@RequestMapping("/jsp")
	public String catttt() {
		return "basedata/action/action";
	}	
	
	@RequestMapping("/editjsp")
	public String editjsp() {
		return "basedata/action/editAction";
	}
	
	@RequestMapping("/searchjsp")
	public String searchjsp() {
		return "basedata/action/search";
	}
	
	@RequestMapping("/editGyAction") // 增删改
	@ResponseBody
	public Object editUser(String oper, @Valid GyAction gyAcyion,BindingResult result,HttpServletResponse response) throws IOException {
		if(result.hasErrors()){
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return ServerResponse.createByError("参数验证失败",result.getAllErrors());
		}
		
		int status = actionService.editGyAction(oper, gyAcyion);
		if (status == 200) {
			return ServerResponse.createBySuccess();
		}else if(status == 888){
			return ServerResponse.createByError("有外键约束！");
		}else if(status == 600){
			return ServerResponse.createByError("没有权限！");
		}else{
			return ServerResponse.createByError();
		}
	}

	@RequestMapping("/listGyAction")
	@ResponseBody

	public Object listUser(String _search, Integer rows, Integer page, String sord, String sidx, String searchField, String searchString, String searchOper) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		
		/*if (!ValidateUtil.validateQuery(tableName)) {
			return ServerResponse.createByError("没有权限");
		}
*/
		if ("false".equals(_search) || StringUtils.isEmpty(_search))// 列表显示
		{
			PageInfo<GyAction> pageInfo = actionService.listGyAction(page, rows, sord, sidx);
			return GridVO.fromPageInfo(pageInfo);
		} 
		//模糊查询
		else if("true".equals(_search) || StringUtils.isEmpty(_search)){
			PageInfo<GyAction> pageInfo = actionService.findGyAction(page, rows, sord, searchField, searchString, searchOper);
			return GridVO.fromPageInfo(pageInfo);
		}else{
			return null;
		}

	}

}
